package com.caiyouxi.goods.controller;

import com.alibaba.fastjson.JSONObject;
import com.caiyouxi.common.bean.ResData;
import com.caiyouxi.common.control.BaseController;
import com.caiyouxi.common.util.Log;
import com.caiyouxi.goods.model.GoodsInfo;
import com.caiyouxi.goods.service.GoodsInfoService;
import com.caiyouxi.util.NumUtil;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import java.util.Date;


/**
 * 商品相关
 * chensong
 */
@RestController
@RequestMapping("/boss/goods/")
public class GoodsInfoController extends BaseController {

    @Autowired
    private GoodsInfoService goodsInfoService;

    /**创建商品
     * @param request
     * @param response
     * @param bean
     * @return
     */
    @RequestMapping("add_goods_info")
    public ResData add_goods_info(HttpServletRequest request, HttpServletResponse response, @RequestBody GoodsInfo bean)
    {
        bean.setId(Long.valueOf(NumUtil.generate_number_on_int(4)));//商品编号
        bean.setCreate_time(new Date());
        bean.setLast_update_time(new Date());
        goodsInfoService.add_goods_info(bean);
        JSONObject json = new JSONObject();
        json.put("id",bean.getId());
        
        return success(json);
    }

    /**
     * 更新商品
     * @param request
     * @param response
     * @param bean
     * @return
     */
    @RequestMapping("update_goods_info")
    public ResData update_goods_info(HttpServletRequest request, HttpServletResponse response, @RequestBody GoodsInfo bean)
    {
        bean.setLast_update_time(new Date());
        goodsInfoService.update_goods_info(bean);
        JSONObject json = new JSONObject();
        json.put("id",bean.getId());

        return success(json);
    }
    
    /**
     * 获取某个商品信息
     */
    @RequestMapping("select_goods_info")
    public ResData select_goods_info(HttpServletRequest request, HttpServletResponse response, @RequestBody GoodsInfo bean){
        GoodsInfo gInfo = goodsInfoService.select_goodsInfo_by_id(bean.getId());
        JSONObject data = (JSONObject) JSONObject.toJSON(gInfo);
        return success(data);
    }

    /**
     * 删除某个商品
     */
    @RequestMapping("delete_goods_info")
    public ResData delete_goods_info(HttpServletRequest request, HttpServletResponse response, @RequestBody GoodsInfo bean){
        goodsInfoService. delete_goods_info(bean);
        Log.d("商品信息已成功被删除");
        return success(null);
    }

    /**
     * 上架
     */
    @RequestMapping("update_status")
    public ResData update_status(HttpServletRequest request, HttpServletResponse response, @RequestBody GoodsInfo bean){
        Log.d("=============================== 上架 ================================");
        goodsInfoService.update_status(bean);
        return success(null);
    }



}
